package com.iqiyi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iqiyi.domain.*;
import com.iqiyi.entity.MovieVo;
import com.iqiyi.mapper.*;
import com.iqiyi.service.ActorService;
import com.iqiyi.service.CategoryService;
import com.iqiyi.service.MovieCategoryService;
import com.iqiyi.service.MovieService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
* @author 11757
* @description 针对表【movie】的数据库操作Service实现
* @createDate 2022-12-17 12:24:17
*/
@Service
@Slf4j
public class MovieServiceImpl extends ServiceImpl<MovieMapper, Movie>
    implements MovieService{

    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private MovieCategoryMapper movieCategoryMapper;

    @Resource
    private ActorMapper actorMapper;

    @Resource
    private MovieActorMapper movieActorMapper;

    /**
     * 注入movieMapper，使用XML文件中写的方法
     */
    @Resource
    private MovieMapper movieMapper;

    @Override
    public List<MovieVo> findAll() {
        List<MovieVo> movieVos = movieMapper.findAll();
        return movieVos;
    }


    /**
     * 用mybatis-plus实现上面的findAll的效果
     * 两种方式的效果一样
     * @return
     */
    @Override
    public List<MovieVo> findAllMovieInfo() {
        //TODO 1.查出所有电影
        List<Movie> movieList = this.list();
        ArrayList<MovieVo> movieVoList = new ArrayList<>();
        for(Movie movie : movieList){
            MovieVo movieVo = BeanUtil.copyProperties(movie, MovieVo.class);//复制属性到MovieVo

            //TODO 2.在电影、类型中间表中查出有该电影ID的记录
            //TODO 2.1 得到中间数据中电影ID相符合的记录
            List<MovieCategory> movieCategoryList = movieCategoryMapper.selectList(Wrappers.lambdaQuery(MovieCategory.class).eq(MovieCategory::getMovieid, movieVo.getId()));
            List<Category> categories = new ArrayList<>();
            for (MovieCategory mc: movieCategoryList){
                //TODO 2.2. 得到类型符合的记录
                Category category = categoryMapper.selectOne(Wrappers.lambdaQuery(Category.class).eq(Category::getId, mc.getCategoryid()));
                categories.add(category);
            }
            movieVo.setCategories(categories);

            //TODO 3.在电影、演员中间表中查出有该电影ID的记录
            //TODO 3.1 得到中间数据中电影ID相符合的记录
            List<MovieActor> movieActorList = movieActorMapper.selectList(Wrappers.lambdaQuery(MovieActor.class).eq(MovieActor::getMovieid, movie.getId()));
            List<Actor> actors = new ArrayList<>();
            for (MovieActor ma: movieActorList){
                //TODO 3.2. 得演员符合的记录
                Actor actor = actorMapper.selectOne(Wrappers.lambdaQuery(Actor.class).eq(Actor::getId, ma.getActorid()));
                actors.add(actor);
            }
            movieVo.setActors(actors);
            movieVoList.add(movieVo);
        }

        return movieVoList;
    }

    @Override
    public void deleteMovieById(Integer movieId) {
        movieMapper.deleteMovie2(movieId);
    }

    @Override
    public List<MovieVo> findTop3ByCates(Integer movieId) {
        return movieMapper.findTop3ByCates(movieId);
    }

    @Override
    public List<MovieVo> findTop3ByActors(Integer movieId) {
        return movieMapper.findTop3ByActors(movieId);
    }
}




